/*
 * =============================================================================
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <eirikb@google.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Eirik Brandtzæg
 * =============================================================================
 */
package no.eirikb.robotec.terminal;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import no.eirikb.robotec.server.Server;
import no.eirikb.robotec.terminal.element.Element;

/**
 *
 * @author eirikb
 * @author <a href="mailto:eirikb@google.com">eirikb@google.com</a>
 */
public class Terminal {

    private TerminalServer terminalServer;
    private File file;
    private Server server;
    private List<Element> elements;
    private Element currentElement;

    public Terminal(TerminalServer terminalServer, String terminal, Server server) {
        this.terminalServer = terminalServer;
        file = new File("files/" + terminal);
        this.server = server;
        elements = new ArrayList<Element>();
        TerminalFileParser parser = new TerminalFileParser(this);
        parser.parseFile("index");
        if (currentElement != null) {
            currentElement.init();
        }
    }

    public void setCurrentElement(Element currentElement) {
        this.currentElement = currentElement;
    }

    public List<Element> getElements() {
        return elements;
    }

    public Server getServer() {
        return server;
    }

    public void read(int c) {
        if (currentElement != null) {
            currentElement.read(c);
        }
    }

    public TerminalServer getTerminalServer() {
        return terminalServer;
    }

    public File getFile() {
        return file;
    }

    public void nextElement() {
        int id = elements.indexOf(currentElement) + 1;
        if (id >= elements.size()) {
            id = 0;
        }
        currentElement = elements.get(id);
        currentElement.init();
    }

    public void prevElement() {
        int id = elements.indexOf(currentElement) - 1;
        if (id < 0) {
            id = elements.size() - 1;
        }
        currentElement = elements.get(id);
        currentElement.init();
    }
}
